package com.wzr.furerp.employee.controller;

import com.google.gson.Gson;
import com.wzr.furerp.employee.dao.entity.Employee;
import com.wzr.furerp.employee.dao.service.EmployeeService;
import com.wzr.furerp.workshop.service.WorkshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/furerp/employee")
public class EmployeeController {

    private static final String FURERP = "/furerp";

    private static final String EMPLOYEE_MANAGE = FURERP + "/employee/manage";

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private WorkshopService workshopService;

    @RequestMapping(value = "/manage", method = RequestMethod.GET)
    public void employeeManage() {
        return;
    }

    @RequestMapping(value = "/getEmployeeList", method = RequestMethod.GET)
    @ResponseBody
    public String getEmployeeList() {
        List<Employee> employeeList = employeeService.getAll();
        Map<String, List> joRe = new HashMap<>();
        List<Map> jsonArray = new ArrayList<>();
        for (Employee employee : employeeList) {
            Map<String, String> joEp = new HashMap<>();
            joEp.put("epId", String.valueOf(employee.getEpId()));
            joEp.put("epName", employee.getEpName());
            joEp.put("workshop", employee.getWorkshop().getWsName());
            jsonArray.add(joEp);
        }
        if (0 < employeeList.size()) {
            joRe.put("employeeList", jsonArray);
        }
        Gson gson = new Gson();
        return gson.toJson(joRe);
    }


    @RequestMapping(value = "/getepbywsid", method = RequestMethod.GET)
    public void getEmployeeByWsId(@RequestParam("wsId") int wsId, PrintWriter printWriter) {
        List<Employee> toEpL = employeeService.getListByWsId(wsId);
        List<Map> ja = new ArrayList<>();
        for (Employee ep : toEpL) {
            Map<String, String> jo = new HashMap<>();
            jo.put("epId", String.valueOf(ep.getEpId()));
            jo.put("epName", ep.getEpName());
            jo.put("workshop.wsId", String.valueOf(ep.getWorkshop().getWsId()));
            ja.add(jo);
        }
        Gson gson = new Gson();
        printWriter.write(gson.toJson(ja));
        printWriter.flush();
        printWriter.close();
    }


    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String employeeAddView(Model model) {
        model.addAttribute("employee", new Employee());
        model.addAttribute("wsList", workshopService.getAll());
        return FURERP + "/employee/add";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String employeeAddSubmit(Employee employee) {
        employeeService.add(employee);
        return InternalResourceViewResolver.REDIRECT_URL_PREFIX + EMPLOYEE_MANAGE;
    }

    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public String employeeDelete(@RequestParam Integer epId) {
        employeeService.delete(epId);
        return InternalResourceViewResolver.REDIRECT_URL_PREFIX + EMPLOYEE_MANAGE;
    }
}
